import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  //redirect 路由重定向
  { path: "/", redirect: "/home" },
  {
    path: "/home",
    component: () => import("@/views/Home/Movie.vue"),
    children: [
      { path: "/home", redirect: "/home/reying" },
      {
        path: "reying",
        name: "Reying",
        component: () => import("@/views/Home/ReYing.vue"),
      },
      {
        path: "cinema",
        name: "Cinema",
        component: () => import("@/views/Home/Cinema.vue"),
      },
      {
        path: "wait",
        name: "Wait",
        component: () => import("@/views/Home/Wait.vue"),
      },
      {
        path: "classic",
        name: "Classic",
        component: () => import("@/views/Home/Classic.vue"),
      },
    ],
  },
  {
    path: "/video",
    name: "Video",
    component: () => import("@/views/Video.vue"),
  },
  {
    path: "/minivideo",
    name: "MiniVideo",
    component: () => import("@/views/MinVideo.vue"),
  },
  {
    path: "/show",
    name: "Show",
    component: () => import("@/views/Show.vue"),
  },
  {
    path: "/me",
    name: "Me",
    component: () => import("@/views/Me.vue"),
   
  },
  {
    //内容页的路由
    path: "/item/:id", //动态路由
    name: "Item",
    component: () => import("@/views/Item.vue"),
  },
  {
    //登录的路由
    path: "/login", //动态路由
    name: "Login",
    component: () => import("@/views/Login.vue"),
  },
  {
    //城市列表的路由
    path: "/city",
    name: "City",
    component: () => import("@/views/City.vue"),
  },
  {
    //404的路由
    path: "*", //动态路由
    name: "page404",
    component: () => import("@/views/Page404.vue"),
  },
];

const router = new VueRouter({
  mode: "hash",
  routes,
});


//添加全局守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/me") {
    //用户中心 ，需要守卫
    //需要判断登录状态  ,我把登录状态 放到了 sessionStorage 中
    if (sessionStorage.getItem("logintype")) {
      next();
    } else if (from.path != "/login") {
      next("/login");
    }
  } else {
    next();
  }
});
export default router;
